package com.hdc.lw.word_server.controller;

import com.alibaba.fastjson.JSON;
import com.hdc.lw.word_server.po.User;
import com.hdc.lw.word_server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/user")
    public String gerUser(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User(null,username,password,null);
//        System.out.println(user);
        String flag = JSON.toJSONString(userService.getUserByName(user));
        return flag;
    }

    @RequestMapping("/register")
    public String register(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User(null,username,password,null);
//        System.out.println(user);
        String flag = JSON.toJSONString(userService.register(user));
        return flag;
    }

    @RequestMapping("/updatePass")
    public String updatePass(HttpServletRequest request){
        User user = new User();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("oldPassword"));
        String pass = request.getParameter("password");
//        System.out.println(user);
//        System.out.println(pass);
        String data = JSON.toJSONString(userService.updatePass(user,pass));
        return data;
    }
}
